<?php
namespace Swiftx\Component\Bundle;
use Swiftx\Interfaces\Bundle\Storage as StorageInterfaces;
use Swiftx\Bundle\Console\Controller\Interfaces;

/**
 * 模块暂存区
 * @package Swiftx\Bundle\Console
 */
class Storage implements StorageInterfaces{

    /**
     * 缓存目录
     * @var string
     */
    protected $cacheDir;

    /**
     * 构建目录
     * @var string
     */
    protected $buildDir;

    /**
     * 资源目录
     * @var string
     */
    protected $resourceDir;

    /**
     * 资源映射
     * @var string
     */
    protected $resourceUri;

    /**
     * 资源位置
     * @var string
     */
    protected $resourceBase;

    /**
     * 设置缓存目录
     * @param string $path
     */
    public function setCacheDir(string $path){
        if($path[strlen($path)-1] != '/') $path .= '/';
        $this->cacheDir = $path;
    }

    /**
     * 设置构建目录
     * @param string $path
     */
    public function setBuildDir(string $path){
        if($path[strlen($path)-1] != '/') $path .= '/';
        $this->buildDir = $path;
    }

    /**
     * 设置资源目录
     * @param string $path
     */
    public function setResourceDir(string $path){
        if($path[strlen($path)-1] != '/') $path .= '/';
        $this->resourceDir = $path;
    }

    /**
     * 设置资源映射
     * @param string $uri
     * @param string $base
     */
    public function setResourceMap(string $uri, string $base){
        if($base[strlen($base)-1] != '/') $base .= '/';
        if($uri[strlen($uri)-1] != '/') $uri .= '/';
        $this->resourceUri = $uri;
        $this->resourceBase = $base;
    }

    /**
     * 生成资源URI
     * @param string $file
     * @param string[] ...$appends
     * @return string
     * @throws \ErrorException
     */
    public function resourceUri(string $file, string ...$appends):string{
        $appends[] = $file;
        foreach($appends as $value){
            $basePath = $this->resourceBase.$value;
            $targetPath = $this->resourceDir.$value;
            if(file_exists($targetPath)) continue;
            if(!file_exists($basePath))
                throw new \ErrorException('资源文件不存在：'.$basePath);
            if(!is_dir(dirname($targetPath)))
                mkdir(dirname($targetPath) , 0777 ,true);
            copy($basePath, $targetPath);
        }
        return $this->resourceUri.$file;
    }

    /**
     * 获取构建路径
     * @param string $value
     * @return string
     */
    public function getBuildPath(string $value):string{
        return $this->buildDir.$value;
    }

    /**
     * 生成构建文件
     * @param string $path
     * @param string $content
     */
    public function makeBuildFile(string $path, string $content){
        file_put_contents($this->getBuildPath($path), $content);
    }

    /**
     * 删除构建文件
     * @param string $path
     */
    public function unlinkBuildFile(string $path){
        unlink($this->getBuildPath($path));
    }

    /**
     * 读取构建内容
     * @param string $path
     * @return string
     */
    public function readBuildFile(string $path):string{
        return file_get_contents($this->getBuildPath($path));
    }

    /**
     * 获取缓存路径
     * @param string $value
     * @return string
     */
    public function getCachePath(string $value):string{
        return $this->cacheDir.$value;
    }

    /**
     * 生成缓存文件
     * @param string $path
     * @param string $content
     */
    public function makeCacheFile(string $path, string $content){
        file_put_contents($this->getCachePath($path),$content);
    }

    /**
     * 删除缓存文件
     * @param string $path
     */
    public function unlinkCacheFile(string $path){
        unlink($this->getCachePath($path));
    }

    /**
     * 读取缓存内容
     * @param string $path
     * @return string
     */
    public function readCacheFile(string $path):string{
        return file_get_contents($this->getCachePath($path));
    }

}